package com.colorlight.cloud.complete.entity.event;

import com.colorlight.cloud.complete.api.CompleteEntityEvent;
import com.colorlight.cloud.complete.entity.enumerate.CameraType;
import com.colorlight.cloud.complete.entity.enumerate.CameraUploadErrorCode;
import com.colorlight.iot.proto.complete.entity.CompleteEntity;
import com.colorlight.iot.proto.complete.event.CompleteEvent;
import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.Objects;

/**
 * 摄像头截图上报异常上报事件
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Slf4j
public class CameraUploadError implements CompleteEntityEvent {

    /**
     * 错误码 摄像头异常 上传截图异常
     */
    private CameraUploadErrorCode code;

    /**
     * 错误提示
     */
    private String message;

    /**
     * 摄像头类型
     */
    private CameraType cameraType;

    @Override
    public CameraUploadError byteConvertObject(ByteString bytes) {
        CompleteEvent.CameraUploadError cameraUploadError = null;
        try {
            cameraUploadError = CompleteEvent.CameraUploadError.parseFrom(bytes);
        } catch (InvalidProtocolBufferException e) {
            log.error("", e);
        }
        Objects.requireNonNull(cameraUploadError);
        CompleteEntity.CameraUploadErrorCode code1 = cameraUploadError.getCode();
        this.setCode(CameraUploadErrorCode.getCameraUploadErrorCode(code1));
        this.setMessage(cameraUploadError.getMessage());
        CompleteEntity.CameraType cameraType1 = cameraUploadError.getCameraType();
        this.setCameraType(CameraType.getCameraType(cameraType1));
        return this;
    }
}

